package cn.stylefeng.guns.modular.car.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.car.entity.Car;
import cn.stylefeng.guns.modular.car.mapper.CarMapper;
import cn.stylefeng.guns.modular.car.model.params.CarParam;
import cn.stylefeng.guns.modular.car.model.result.CarResult;
import  cn.stylefeng.guns.modular.car.service.CarService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 车辆信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-05-10
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {

    @Override
    public void add(CarParam param){
        Car entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CarParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CarParam param){
        Car oldEntity = getOldEntity(param);
        Car newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CarResult findBySpec(CarParam param){
        return null;
    }

    @Override
    public List<CarResult> findListBySpec(CarParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CarParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CarParam param){
        return param.getSeqId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Car getOldEntity(CarParam param) {
        return this.getById(getKey(param));
    }

    private Car getEntity(CarParam param) {
        Car entity = new Car();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
